package com.inossem.utils.upload;

import com.inossem.utils.log.LogUtils;
import com.yanzhenjie.nohttp.FileBinary;
import com.yanzhenjie.nohttp.NoHttp;
import com.yanzhenjie.nohttp.OnUploadListener;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.error.NetworkError;
import com.yanzhenjie.nohttp.error.ServerError;
import com.yanzhenjie.nohttp.error.StorageReadWriteError;
import com.yanzhenjie.nohttp.error.StorageSpaceNotEnoughError;
import com.yanzhenjie.nohttp.error.TimeoutError;
import com.yanzhenjie.nohttp.error.URLError;
import com.yanzhenjie.nohttp.error.UnKnownHostError;
import com.yanzhenjie.nohttp.rest.Request;
import com.yanzhenjie.nohttp.rest.RequestQueue;
import com.yanzhenjie.nohttp.rest.SimpleResponseListener;

import java.io.File;
import java.util.Map;

/**
 * 作者：wsy on 2017/11/16 15:24
 * <p>
 * 邮箱：445979770@qq.com
 */

public class UpLoadUtils {
    private static UpLoadUtils instance;
    public static final int UPLOAD_IMAGE_REQUEST_CODE = 8888;
    public static final String UPLOAD_IMAGE_KEY = "file";
    public static final int UPLOAD_COUNT = 1;
    private static RequestQueue queue;

    public static UpLoadUtils getInstance() {
        if (instance == null)
            synchronized (UpLoadUtils.class) {
                if (instance == null)
                    instance = new UpLoadUtils();
            }
        return instance;
    }


    private UpLoadUtils() {
        queue = NoHttp.newRequestQueue(UPLOAD_COUNT);
    }

    public <T> void request(int what, Request<T> request, SimpleResponseListener<T> listener) {
        LogUtils.i("what" + what);
        queue.add(what, request, listener);
    }

    // 完全退出app时，调用这个方法释放CPU。
    public void stop() {
        queue.stop();
    }

    public static Request<String> getUploadRequest(int what ,String url,File file, OnUploadListener onUploadListener){
        return getUploadRequest(what,url,null,file,onUploadListener);
    }

    public static Request<String> getUploadRequest(int what ,String url, Map<String, String> header, File file, OnUploadListener onUploadListener) {
        LogUtils.i("url ===== " + url);
        Request<String> request = NoHttp.createStringRequest(url, RequestMethod.POST);
//        return  NoHttp.createStringRequest("http://192.168.0.7:8080/biz/zljy/uploadFile", RequestMethod.POST);
        // 添加普通参数。
        request.add("user", "yolanda");

        // 上传文件需要实现NoHttp的Binary接口，NoHttp默认实现了FileBinary、InputStreamBinary、ByteArrayBitnary、BitmapBinary。
        FileBinary fileBinary0 = new FileBinary(file);
        /**
         * 监听上传过程，如果不需要监听就不用设置。
         * 第一个参数：what，what和handler的what一样，会在回调被调用的回调你开发者，作用是一个Listener可以监听多个文件的上传状态。
         * 第二个参数： 监听器。
         */
        fileBinary0.setUploadListener(what, onUploadListener);

        request.add(UPLOAD_IMAGE_KEY, fileBinary0);// 添加1个文件
//        if(header==null){
//            throw new InossemException("头文件不可以为空");
//        }
        if(header!=null){
            for (String key : header.keySet()) {
                request.addHeader(key, header.get(key));
            }
        }
        return request;
    }

    public static String getErroMessage(Exception exception) {
        String result;
        if (exception instanceof ServerError) {
            result = "服务器数据错误";
        } else if (exception instanceof NetworkError) {
            result = "网络不可用，请检查网络";
        } else if (exception instanceof StorageReadWriteError) {
            result = "存储卡错误，请检查存储卡";
        } else if (exception instanceof StorageSpaceNotEnoughError) {
            result = "存储空间不足";
        } else if (exception instanceof TimeoutError) {
            result = "下载超时";
        } else if (exception instanceof UnKnownHostError) {
            result = "找不到服务器";
        } else if (exception instanceof URLError) {
            result = "URL地址错误";
        } else {
            result = "未知错误";
        }
        return result;
    }
}
